package com.zcs.app.wisdomschool.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.greateffect.littlebud.lib.app.ActivityStackManger;
import com.greateffect.littlebud.lib.base.BaseNormalActivityAdv;
import com.greateffect.littlebud.lib.cache.CustomExtraKeys;
import com.greateffect.littlebud.lib.imageloader.ImageLoader;
import com.greateffect.littlebud.lib.utils.GlideStaticUtils;
import com.greateffect.littlebud.lib.utils.JLogUtil;
import com.greateffect.littlebud.lib.utils.MyTextUtils;
import com.zcs.app.wisdomschool.R;
import com.zcs.app.wisdomschool.bean.EvaluateBean;
import com.zcs.lib.utils.RequestFocusUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * Created by ZengCS on 2019/1/18.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
@EActivity(R.layout.activity_apply_input)
public class ApplyInputActivity extends BaseNormalActivityAdv {
    @Extra(CustomExtraKeys.KEY_SERIALIZABLE_BEAN)
    EvaluateBean mEvaluateBean;

    // 请修改对应标题
    private static final String TITLE = "申请填报";
    // 以下代码请勿修改
    @ViewById(R.id.id_tv_custom_toolbar_title)
    TextView tvTitle;
    @ViewById(R.id.id_tv_apply_name)
    TextView tvName;
//    @ViewById(R.id.id_iv_apply_pic)
//    ImageView ivPic;
    @ViewById(R.id.id_et_desc)
    EditText etDesc;
    @ViewById(R.id.id_et_phone)
    EditText etPhone;
    @ViewById(R.id.id_btn_select_file)
    TextView tvFileName;

    @AfterViews
    void init() {
        tvTitle.setText(TITLE);
        RequestFocusUtil.focus(tvTitle);
        // 设置Toolbar顶部Padding
        setToolbarPaddingWhenTransStatusBar();
        if (mEvaluateBean == null) {
            showExitMessageDialog("数据异常");
            return;
        }

        tvName.setText(mEvaluateBean.getName());
//        new Handler().postDelayed(() -> {
//            // 加载图片
//            GlideStaticUtils.displayImage(mEvaluateBean.getPic(), R.drawable.empty_photo, ivPic);
//        }, 200);
    }

    // 处理点击事件
    @Click({
            R.id.id_iv_custom_toolbar_back,
            R.id.id_btn_select_file,
            R.id.id_btn_submit,
    })
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_btn_select_file:
                openFileSelector();
                break;
            case R.id.id_btn_submit:
                onSubmit();
                break;
            case R.id.id_iv_custom_toolbar_back:
                onBackPressed();
                break;
        }
    }

    private void openFileSelector() {
        //调用系统文件管理器打开指定路径目录
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
            // Uri uri = data.getData();
            tvFileName.setText("已上传");
        }
    }

    private void onSubmit() {
        if ("点击上传".equals(tvFileName.getText())) {
            showToast("请上传申报材料");
            return;
        }
        String desc = etDesc.getText().toString().trim();
        if (TextUtils.isEmpty(desc)) {
            showToast("请填写申报说明");
            return;
        }
        String phone = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号码");
            return;
        }
        if (!MyTextUtils.isPhoneNumber(phone)) {
            showToast("请输入正确的手机号码");
            return;
        }

        showLoading("正在提交申请...");
        new Handler().postDelayed(() -> {
            showToast("申请已提交，请耐心等待通知。");
            killMyself();
            ActivityStackManger.getInstance().finishActivity(ApplyDetailActivity_.class);
        }, 1500);
    }
}
